package Pet.Management.controller;

import Pet.Management.domain.entity.Payment;
import Pet.Management.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/api/payments")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    /**
     * 创建支付记录
     */
    @PostMapping
    public ResponseEntity<Payment> createPayment(@RequestBody Payment payment) {
        return ResponseEntity.ok(paymentService.createPayment(payment));
    }

    /**
     * 更新支付状态
     */
    @PutMapping("/{paymentId}/status")
    public ResponseEntity<Payment> updatePaymentStatus(
            @PathVariable Long paymentId,
            @RequestBody Map<String, String> request) {
        String status = request.get("status");
        return ResponseEntity.ok(paymentService.updatePaymentStatus(paymentId, status));
    }

    /**
     * 获取支付记录
     */
    @GetMapping("/{paymentId}")
    public ResponseEntity<Payment> getPayment(@PathVariable Long paymentId) {
        return ResponseEntity.ok(paymentService.getPayment(paymentId));
    }

    /**
     * 获取用户的支付记录
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<Payment>> getUserPayments(
            @PathVariable Long userId,
            Pageable pageable) {
        return ResponseEntity.ok(paymentService.getUserPayments(userId, pageable));
    }

    /**
     * 获取订单的支付记录
     */
    @GetMapping("/order/{orderId}")
    public ResponseEntity<Payment> getOrderPayment(@PathVariable Long orderId) {
        return ResponseEntity.ok(paymentService.getOrderPayment(orderId));
    }

    /**
     * 验证支付金额
     */
    @GetMapping("/order/{orderId}/validate")
    public ResponseEntity<Boolean> validatePaymentAmount(
            @PathVariable Long orderId,
            @RequestParam Double amount) {
        return ResponseEntity.ok(paymentService.validatePaymentAmount(orderId, amount));
    }

    /**
     * 处理支付回调
     */
    @PostMapping("/callback")
    public ResponseEntity<Payment> handlePaymentCallback(@RequestBody Map<String, String> request) {
        String outTradeNo = request.get("outTradeNo");
        String transactionId = request.get("transactionId");
        String status = request.get("status");
        return ResponseEntity.ok(paymentService.handlePaymentCallback(outTradeNo, transactionId, status));
    }

    /**
     * 申请退款
     */
    @PostMapping("/{paymentId}/refund")
    public ResponseEntity<Payment> refund(
            @PathVariable Long paymentId,
            @RequestBody Map<String, Object> request) {
        Double amount = ((Number) request.get("amount")).doubleValue();
        String reason = (String) request.get("reason");
        return ResponseEntity.ok(paymentService.refund(paymentId, amount, reason));
    }

    /**
     * 取消支付
     */
    @PostMapping("/{paymentId}/cancel")
    public ResponseEntity<Payment> cancelPayment(@PathVariable Long paymentId) {
        return ResponseEntity.ok(paymentService.cancelPayment(paymentId));
    }

    /**
     * 获取支付详情
     */
    @GetMapping("/{paymentId}/details")
    public ResponseEntity<Payment> getPaymentDetails(@PathVariable Long paymentId) {
        return ResponseEntity.ok(paymentService.getPaymentDetails(paymentId));
    }
} 